<template>
  <van-icon
    :class="{collected:value}"
    :loading="loading"
    :name=" value ? 'star':'star-o'"
    @click="onCollect"
  />
</template>


<script>
import { CollectArticles, deleteCollect } from '@/api/article'

export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        if (this.value) {
          // 已收藏
          const { data } = await deleteCollect(this.articleId)
          console.log(data)
        } else {
          // 未收藏
          const { data } = await CollectArticles(this.articleId)
          console.log(data)
        }
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast.fail('操作失败')
      }

      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.collected {
  color: #ffa500;
}
</style>